package com.ptswitch.material.pages.contract;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.SplitPageInfo;
import org.slf4j.Logger;

import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.ContractService;
import com.ptswitch.material.util.security.PermissionUtil;

public class SearchContract extends PageBase {
    @Inject
    private Logger logger;
    @Inject
    private ContractService contractSer;
    @Property
    private List<Contract> list;
    @Property
    private String like = "";
    @Property
    private SplitPageInfo page = new SplitPageInfo();

    public Object withInit(String like, long size, long first) {
        this.like = like;
        page.setCurrentPage(first);
        page.setPageSize(size);
        return this;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(String like, long size, long first) {
        checkPermitted(PermissionUtil.ContractSearch);
        withInit(like, size, first);
        return null;
    }

    @SetupRender
    void display() {
        page.setRowCount(contractSer.countLike(like));
        list = contractSer.findLike(like, page.getCurrentRow(), page.getPageSize());
//		page.setRowCount(materialSer.countSearch(like));
//		list = materialSer.search(like, page.getCurrentRow(), page.getPageSize());
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return new Object[] { like, page.getPageSize(), page.getCurrentPage() };
    }
}
